package com.hducms.cms.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hducms.cms.dao.CmsGroupDao;
import com.hducms.cms.entity.main.Channel;
import com.hducms.cms.entity.main.CmsGroup;
import com.hducms.cms.service.ChannelService;
import com.hducms.cms.service.CmsGroupService;

@Service
@Transactional
public class CmsGroupServiceImpl implements CmsGroupService {
	@Transactional(readOnly = true)
	public List<CmsGroup> getList() {
		return dao.getList();
	}

	@Transactional(readOnly = true)
	public CmsGroup findById(Integer id) {
		CmsGroup entity = dao.findById(id);
		return entity;
	}

	@Transactional(readOnly = true)
	public CmsGroup getRegDef() {
		return dao.getRegDef();
	}

//	public void updateRegDef(Integer regDefId) {
//		if (regDefId != null) {
//			for (CmsGroup g : getList()) {
//				if (g.getId().equals(regDefId)) {
//					g.setRegDef(true);
//				} else {
//					g.setRegDef(false);
//				}
//			}
//		}
//	}
//
//	public CmsGroup save(CmsGroup bean) {
//		bean.init();
//		dao.save(bean);
//		return bean;
//	}
	
	public CmsGroup save(CmsGroup bean,Integer[] viewChannelIdss, Integer[] contriChannelIds){
		bean.init();
		dao.save(bean);
		if (viewChannelIdss != null && viewChannelIdss.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", bean.getId());
			Set<Integer> list = new HashSet<Integer>();
			for (Integer gid : viewChannelIdss) {
				list.add(gid);
			}
			map.put("list", list);
			dao.insertViewGroup(map);
		}
		if (contriChannelIds != null && contriChannelIds.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", bean.getId());
			Set<Integer> list = new HashSet<Integer>();
			for (Integer gid : contriChannelIds) {
				list.add(gid);
			}
			map.put("list", list);
			dao.insertContriGroup(map);

		}
		return bean;
	}

//	public CmsGroup update(CmsGroup bean) {
//		Updater<CmsGroup> updater = new Updater<CmsGroup>(bean);
//		CmsGroup entity = dao.updateByUpdater(updater);
//		return entity;
//	}
//	
	public CmsGroup update(CmsGroup bean,Integer[] viewChannelIds, Integer[] contriChannelIds){

		dao.update(bean);
		// 更新浏览栏目权限
		Set<Channel> viewChannels = bean.getViewChannels();
		// 清除
		dao.deleteViewChannelIds(bean.getId());
		if (viewChannelIds != null && viewChannelIds.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", bean.getId());
			Set<Integer> list = new HashSet<Integer>();
			for (Integer gid : viewChannelIds) {
				list.add(gid);
			}
			map.put("list", list);
			dao.insertViewGroup(map);
		}
		
		//清除
		dao.deleteContriChannelIds(bean.getId());
		if (contriChannelIds != null && contriChannelIds.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", bean.getId());
			Set<Integer> list = new HashSet<Integer>();
			for (Integer gid : contriChannelIds) {
				list.add(gid);
			}
			map.put("list", list);
			dao.insertContriGroup(map);
		}
//		for (Channel channel : viewChannels) {
//			channel.getViewGroups().remove(bean);
//		}
//		bean.getViewChannels().clear();
//		Set<Channel>contriChannels=bean.getContriChannels();
		//清除
//		for(Channel channel:contriChannels){
//			channel.getContriGroups().remove(bean);
//		}
//		bean.getContriChannels().remove(bean);
//		Channel c;
//		if (viewChannelIds != null && viewChannelIds.length > 0) {
//			for (Integer cid : viewChannelIds) {
//				c = channelMng.findById(cid);
//				bean.addToViewChannels(c);
//			}
//		}
//		if (contriChannelIds != null && contriChannelIds.length > 0) {
//			for (Integer cid : contriChannelIds) {
//				c = channelMng.findById(cid);
//				bean.addToContriChannels(c);
//			}
//		}
		return bean;
	}

	public CmsGroup deleteById(Integer id) {
		CmsGroup bean = findById(id);
		dao.deleteById(id);
		return bean;
	}

	public CmsGroup[] deleteByIds(Integer[] ids) {
		CmsGroup[] beans = new CmsGroup[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

//	public CmsGroup[] updatePriority(Integer[] ids, Integer[] priority) {
//		int len = ids.length;
//		CmsGroup[] beans = new CmsGroup[len];
//		for (int i = 0; i < len; i++) {
//			beans[i] = findById(ids[i]);
//			beans[i].setPriority(priority[i]);
//		}
//		return beans;
//	}
	
	@Override
	public Set<Integer> getViewChannelIds(Integer groupId) {
		return dao.getViewChannelIds(groupId) ;
	}

	@Override
	public Set<Integer> getContriChannelIds(Integer groupId) {
		return  dao.getContriChannelIds(groupId);
	}
	
	private CmsGroupDao dao;
	@Autowired
	private ChannelService channelMng;

	@Autowired
	public void setDao(CmsGroupDao dao) {
		this.dao = dao;
	}

}